﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {


        static void Main(string[] args)
        {
            String threadId = "";
            String msHost = "";
            int msPort = 2526;

            Encoding enc = Encoding.UTF8;
            string recvMsg;

            StreamWriter writer =
                new StreamWriter(@"C:\jkLog.log",
                    true,
                    enc);

            using (TcpClient client = new TcpClient(msHost, msPort))
            {
                writeLog("Connect : " + msHost + ", " + msPort, writer);

                using (NetworkStream networkStream = client.GetStream())
                {
                    string message = "<thread thread=\"" + threadId + "\" version=\"20061206\" res_from=\"-1\" />\0"; // 接続
                    byte[] data = enc.GetBytes(message);

                    if (networkStream.CanWrite)
                    {
                        writeLog("Write Server Message", writer);
                        networkStream.Write(data, 0, data.Length);
                        networkStream.Flush();
                    }
                    else
                    {
                        writeLog("Don't Write!!", writer);
                        return;
                    }

                    Regex regex = new Regex(
                        "(<chat).*?(</chat>)",
                        System.Text.RegularExpressions.RegexOptions.IgnoreCase);

                    byte[] recvBytes;

                    String total = "";

                    while (networkStream.CanRead)
                    {
                        recvBytes = new byte[512];
                        if (networkStream.Read(recvBytes, 0, recvBytes.Length) != 0)
                        {
                            recvMsg = enc.GetString(recvBytes);
                            total = total + recvMsg;

                            if (regex.IsMatch(total))
                            {
                                MatchCollection collect = regex.Matches(total);

                                foreach (Match item in collect) {
                                    writeLog("RECEIVE : " + item.Value, writer);
                                    System.Console.WriteLine("RECEIVE : " + item.Value);
                                }

                                // 一度取得したものは消しちゃう
                                total = regex.Replace(total, "");
                            }
                        }
                    }
                }
            }


            writer.Close();
        }

        static void writeLog(String msg, StreamWriter writer)
        {
            writer.WriteLine("[" + DateTime.Now + "]" + msg);
        }
    }
}
